﻿using MainFrameDesign.Action;
using MainFrameDesign.Draws;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MainFrameDesign.ContentContorl
{
   public class Dom:Div
   {
       public delegate void OnHeightChangeDelegate(int height);
       public event OnHeightChangeDelegate OnHeightChange;
       [Browsable(false)]
       public int WindowHeight { get; set; }
       public bool IsDebug = false;
       public Dom() : base() 
       {
           Padding margin = new Padding();
           margin.All = 20;
           this.Margin= margin;
           Padding padding = new Padding();
           padding.All = 10;
           padding.Top = 30;
           this.Padding= padding;

           this.Parent = null;
           this.Root = this;
           this.Location.X = 0;
           this.Location.Y = 0;
           this.Name = "Root";
       }

       public override Div After(Div div,bool isCallDraw)
       {
           throw new Exception("根元素不能调用After方法");
       }

       public override Div Before(Div div, bool isCallDraw)
       {
           throw new Exception("根元素不能调用Before方法");
       }

       public override Div AppendChild(Div div, bool isCallDraw)
       {
           div.Root = this;
           div.Parent = this;
           this.Childs.Add(div);
           if (isCallDraw) 
           {
               CallDrawPar();
           }
           return div;
       }

       /// <summary>
       /// 调用绘制参数
       /// </summary>
       public void CallDrawPar() 
       {
           CallHeight();
           CallWidth();
           CallPoint();
           CallRect();
           int height = this.Height - this.WindowHeight;
           if (OnHeightChange != null) 
           {
               OnHeightChange(height);
           }
       }

       public override Boolean IsInTop(Point P) 
       {
           return false;
       }

       public override Boolean IsInBottom(Point P) 
       {
           return false;
       }

       public override void CallWidth()
       {
           foreach (var item in Childs)
           {
               item.CallWidth();
           }
       }

       public Dom Resize(int width, int height) 
       {
           this.Width = width;
           this.WindowHeight = height;
           CallDrawPar();
           return this;
       }

       public override void Draw(Graphics g)
       {
           g.Clear(Color.White);
           base.Draw(g);
       }

       public override Div IsHover(Point P, ref AppendEnum appendenum)
       {
           Div div = null;
           appendenum = AppendEnum.None;
           if (IsInContent(P))
           {
               div = this;
               appendenum = AppendEnum.Content;
               foreach (var item in Childs)
               {
                   div = item.IsHover(P, ref appendenum);
                   if (div != null) 
                   {
                       break;
                   }
               }
           }
           return div;
       }

       public override void DrawDrawRect(Graphics g)
       {
           g.DrawRectangle(MyDraw.BorderPen, DrawRect);
       }

       public override void DrawContent(Graphics g)
       {
           return;
       }
   }
}
